package com.mingnew.ecache.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingnew.ecache.entity.Employee;
import com.mingnew.ecache.entity.dto.EmployeeDto;
import com.mingnew.ecache.entity.dto.PageDto;
import com.mingnew.ecache.entity.vo.EmployeeVo;
import com.mingnew.ecache.mapper.EmployeeMapper;
import com.mingnew.ecache.service.EmployeeService;
import com.mingnew.ecache.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.Objects;

/**
 * @author : lk
 * @date 2021/5/2 12:05
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@CacheConfig(cacheNames = "employee", cacheManager = "myCacheManager")
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result selectList(EmployeeDto employeeDto) {
        // 分页对象
        Page<EmployeeVo> page = new Page<>(employeeDto.getCurrentPage(), employeeDto.getLimit());
        // 将后端查询到的数据放入到分页返回对象中
        Page<EmployeeVo> employeeVoPage = page.setRecords(employeeMapper.selectEmployeeList(page,employeeDto));
        return Result.success(employeeVoPage);
    }

    @Override
    @Cacheable(key = "#id")
    public EmployeeVo getEmp(Integer id) {
        EmployeeVo emp = employeeMapper.getEmp(id);
        return emp;
    }

    @Override
    @CachePut(key = "#employeeVo.id")
    public EmployeeVo updateEmployeeById(EmployeeVo employeeVo) {
        Employee target = new Employee();
        BeanUtils.copyProperties(employeeVo, target);
        employeeMapper.updateById(target);
        return employeeVo;
    }

    @Override
    @CacheEvict(allEntries = true)
    public void deleteEmployee(Integer id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public List<EmployeeVo> searchEmployee(Employee employee) {
        return null;
    }


}
